package com.quectel.core.module.scheduling.service.impl;


import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.quectel.constant.global.SystemConstants;
import com.quectel.core.constants.RedisCacheConstants;
import com.quectel.core.module.scheduling.dao.SchedulingCalendarConfigBindingDao;
import com.quectel.core.module.scheduling.dto.SchedulingCalendarConfigBindingDto;
import com.quectel.core.module.scheduling.entity.SchedulingCalendarConfigBindingEntity;
import com.quectel.core.module.scheduling.service.SchedulingCalendarConfigBindingService;
import com.quectel.util.common.CopyUtils;
import com.quectel.util.common.DateUtils;
import com.quectel.util.kit.CacheKit;
import com.quectel.util.kit.Snowflake;
import com.quectel.util.mybatis.MyBatisPlusUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;
/**
 *
 * @author zhangheng
 * @email nolan.zhang@quectel.com
 * @date 2023/07/05 13:24
 */
@DubboService
public class SchedulingCalendarConfigBindingServiceImpl implements SchedulingCalendarConfigBindingService{

    private static final Logger LOGGER = LoggerFactory.getLogger(SchedulingCalendarConfigBindingServiceImpl.class);

    /**
     * 入参解释:实体id
     */
    private static final Function<Long, String> SCHEDULING_CALENDAR_CONFIG_BINDING_CACHE_BY_ID_FUNC = id -> String.format(RedisCacheConstants.PROJECT_NAME + "SCHEDULING_CALENDAR_CONFIG_BINDING_CACHE_BY_ID:%d", id);

    /**
     * 清除缓存方法
     */
    private static final Consumer<SchedulingCalendarConfigBindingDto> CLEAR_CACHE_FUNC = dto -> {
        CacheKit.invalidRedisCache(SCHEDULING_CALENDAR_CONFIG_BINDING_CACHE_BY_ID_FUNC.apply(dto.getId()));
    };

    @Autowired
    private SchedulingCalendarConfigBindingDao schedulingCalendarConfigBindingDao;
    @Autowired
    private SchedulingCalendarConfigBindingService schedulingCalendarConfigBindingService;


    /**
     * @param calendarId
     * @param type
     * @param dateList
     */
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void updateBindDate(Long calendarId, Byte type, List<String> dateList) {
        // 删除绑定关系1-考勤组负责人
        schedulingCalendarConfigBindingDao.delete(new LambdaQueryWrapper<SchedulingCalendarConfigBindingEntity>()
                .eq(SchedulingCalendarConfigBindingEntity::getSchedulingCalendarConfigId, calendarId)
                .eq(SchedulingCalendarConfigBindingEntity::getType, type)
        );

        for (String date: dateList) {
            Date parseDate = DateUtil.parse(date, DateUtils.FormatType.SIMPLE.getType());
            SchedulingCalendarConfigBindingDto addly= new SchedulingCalendarConfigBindingDto();
            addly.setId(Snowflake.nextId());
            addly.setSchedulingCalendarConfigId(calendarId);
            addly.setType(type);
            addly.setDate(parseDate);
            schedulingCalendarConfigBindingService.save(addly);
        }
    }

    /**
     * @param configId
     * @return
     */
    @Override
    public List<SchedulingCalendarConfigBindingDto> selectListByConfigId(Long configId) {
        List<SchedulingCalendarConfigBindingEntity> list = schedulingCalendarConfigBindingDao.selectList(
                new LambdaQueryWrapper<SchedulingCalendarConfigBindingEntity>()
                .eq(SchedulingCalendarConfigBindingEntity::getSchedulingCalendarConfigId, configId)
        );
        return CopyUtils.copyList(list, SchedulingCalendarConfigBindingDto.class);
    }

    @Override
    public SchedulingCalendarConfigBindingDto selectCacheById(Long id) {
        return CacheKit.cacheToRedis(
                () -> selectById(id),
                SCHEDULING_CALENDAR_CONFIG_BINDING_CACHE_BY_ID_FUNC.apply(id),
                SystemConstants.NOT_NULL_CACHE_EXPIRE_SECONDS
        );
    }

    @Override
    public SchedulingCalendarConfigBindingDto selectById(Long id){
            SchedulingCalendarConfigBindingEntity entity = schedulingCalendarConfigBindingDao.selectById(id);
        return paddingField(CopyUtils.copyObj(entity,SchedulingCalendarConfigBindingDto.class));
    }

    @Override
    public List<SchedulingCalendarConfigBindingDto> queryList(Map<String,Object> params){
        List<SchedulingCalendarConfigBindingEntity> list = schedulingCalendarConfigBindingDao.queryList(params);
        List<SchedulingCalendarConfigBindingDto> result =  CopyUtils.copyList(list,SchedulingCalendarConfigBindingDto.class);
        result.forEach(this::paddingField);
        return result;
    }

    @Override
    public List<SchedulingCalendarConfigBindingDto> queryListWithoutPadding(Map<String,Object> params){
        List<SchedulingCalendarConfigBindingEntity> list = schedulingCalendarConfigBindingDao.queryList(params);
        return CopyUtils.copyList(list,SchedulingCalendarConfigBindingDto.class);
    }

    /**
     * 此方法慎用 禁止填充List一类 比如一个人有多个地址这里不允许填充
     * 填充的原则是: 1:被填充对象不经常变换 2:根据id可以走缓存 3数据足够简单 比如设备类型 所属园区等
     *
     * @param dto
     * @return
     */
    private SchedulingCalendarConfigBindingDto paddingField(SchedulingCalendarConfigBindingDto dto){
        if (dto != null){

        }
        return dto;
    }

    @Override
    public int queryTotal(Map<String, Object> params){
        return schedulingCalendarConfigBindingDao.queryTotal(params);
    }

    @Override
    public Long save(SchedulingCalendarConfigBindingDto dto){
        SchedulingCalendarConfigBindingEntity entity = CopyUtils.copyObj(dto,SchedulingCalendarConfigBindingEntity.class);
        entity.setId(Snowflake.nextId());
        schedulingCalendarConfigBindingDao.insert(entity);
        return entity.getId();
    }

    @Override
    public void updateById(SchedulingCalendarConfigBindingDto dto){
        SchedulingCalendarConfigBindingEntity entity = CopyUtils.copyObj(dto,SchedulingCalendarConfigBindingEntity.class);

        schedulingCalendarConfigBindingDao.updateById(entity);

        CLEAR_CACHE_FUNC.accept(dto);
    }

    @Override
    public void updateAllColumnById(SchedulingCalendarConfigBindingDto dto) {
       SchedulingCalendarConfigBindingEntity entity = CopyUtils.copyObj(dto,SchedulingCalendarConfigBindingEntity.class);

       MyBatisPlusUtils.updateAllColumnById(entity, schedulingCalendarConfigBindingDao);

       CLEAR_CACHE_FUNC.accept(dto);
    }

    @Override
    public void deleteById(Long id){

        CLEAR_CACHE_FUNC.accept(selectById(id));

        schedulingCalendarConfigBindingDao.deleteById(id);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] ids){
        for (Long id : ids) {
            deleteById(id);
        }
    }
}
